package text111;

import java.util.Stack;

public class Solution24 {
	public ListNode getKthFromEnd(ListNode head, int k) {
		/*
		 * ListNode p = head;
		 * 
		 * Stack<Integer> tmp = new Stack<Integer>(); Stack<Integer> res = new
		 * Stack<Integer>(); while(head!=null) { tmp.add(head.val); head = head.next; }
		 * System.out.println(tmp.toString()); int i = 0; while(i<k) {
		 * res.add(tmp.pop()); i++; } System.out.println(res.toString());
		 * 
		 * while(!res.empty()) { if (p.next!=null) { p.val = res.pop(); p=p.next; }
		 * 
		 * } p.next = null; return p;
		 */
		 ListNode pNode = head;
	        ListNode qNode = head;
			int t = 0;
			while(head!=null) {
				
				head = head.next;
	            t++;
			}
	        System.out.println(t-k);
			for (int i = 0; i < (t-k); i++) {
				qNode = qNode.next;
			}
	        pNode = qNode;
			return pNode;
    }
}
